package com.dp.service.impl3;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.JsonPage;
import com.dp.util.DataMap;
import com.dp.vo.NewsPlatforminfo;

@Service
@Transactional
public class PlatforminfoServiceImpl3 extends BaseImpl {
	
	/**
	 * 已调用
	 * Find all the NewsPlatforminfo Entity
	 * @param hashMap
	 * @param firstrow
	 * @param maxrow
	 * @return List<NewsPlatforminfo>
	 */
	public List<NewsPlatforminfo> findAllPlatforminfoEntity(DataMap hashMap, Integer firstrow,
			Integer maxrow){
		JsonPage jpage = (JsonPage)hashMap.get("jpage");
		NewsPlatforminfo platforminfo=(NewsPlatforminfo) hashMap.get("platforminfo");
		List<NewsPlatforminfo> queryList=null;
		try {
			StringBuffer sf = new StringBuffer("SELECT n FROM NewsPlatforminfo n WHERE 1=1 ");
			if(platforminfo!=null){
				if(platforminfo.getMplatform()!=null&& platforminfo.getMplatform().toString().trim().length() > 0){
					sf.append(" AND n.mplatform like '%"
							+ platforminfo.getMplatform().toString().trim() + "%'");
				}
				if(platforminfo.getMagent()!=null&& platforminfo.getMagent().toString().trim().length() > 0){
					sf.append(" AND n.magent like '%"
							+ platforminfo.getMagent().toString().trim() + "%'");
				}
				if(platforminfo.getMserialnum()!=null&& platforminfo.getMserialnum().toString().trim().length() > 0){
					sf.append(" AND n.mserialnum like '%"
							+ platforminfo.getMserialnum().toString().trim() + "%'");
				}
			}
			Object role_id = hashMap.get("role_id");
		    Object user_id = hashMap.get("user_id");
		    if(StringIsNotNull(role_id) && StringIsNotNull(user_id))
		    {
				sf.append(" AND userid='" + user_id + "'");
		    }
			
			if(jpage!=null){
				if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
					sf.append(" order by n."+jpage.getSidx());
					if("mserialnum".equals(jpage.getSidx())){
						sf.append("*1");
					}		
				    sf.append(" "+jpage.getSord());
				}
			}
			System.out.println(sf.toString());
			Query query = em.createQuery(sf.toString());
			if (firstrow != null) {
				query.setFirstResult(firstrow);
			}
			if (maxrow != null) {
				query.setMaxResults(maxrow);
			}
			queryList = query.getResultList();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return queryList;
	}


	/**
	 * 已调用
	 * Update NewsPlatforminfo Entity
	 * @param NewsPlatforminfo
	 * @return Boolean
	 */
	public Boolean updateNewsPlatforminfo(NewsPlatforminfo platforminfo) {
		Boolean resultb=true;
			String jpql="SELECT n FROM NewsPlatforminfo n WHERE n.mplatform=:mplatform AND n.magent=:magent AND n.mserialnum=:mserialnum AND platformid!=:platformid";
			Query query = em.createQuery(jpql);
			query.setParameter("mplatform", platforminfo.getMplatform());
			query.setParameter("magent", platforminfo.getMagent());
			query.setParameter("mserialnum", platforminfo.getMserialnum());
			query.setParameter("platformid", platforminfo.getPlatformid());
			NewsPlatforminfo platforminfo_temp= (NewsPlatforminfo) getSingleResult(query);
			if(platforminfo_temp!=null){
				resultb=null;
			}else{
				jpql="UPDATE NewsPlatforminfo n SET n.mplatform=:mplatform,n.magent=:magent,n.mserialnum=:mserialnum,n.note=:note" +
				" WHERE n.platformid=:platformid";
				query = em.createQuery(jpql);
				query.setParameter("platformid", platforminfo.getPlatformid());
				query.setParameter("mplatform", platforminfo.getMplatform());
				query.setParameter("magent", platforminfo.getMagent());
				query.setParameter("mserialnum", platforminfo.getMserialnum());
				query.setParameter("note", platforminfo.getNote());
				int iCode=query.executeUpdate();
				if(iCode<0){
					resultb=false;
				}
			}
		
		return resultb;
	}



	/***
	 * 已调用
	 * addNewsPlatforminfo
	 * @param platforminfo
	 * @return Boolean
	 */
	public Boolean addNewsPlatforminfo(NewsPlatforminfo platforminfo) {
		Boolean resultb=true;
		try {
			String jpql="SELECT n FROM NewsPlatforminfo n WHERE n.mplatform=:mplatform AND n.magent=:magent AND n.mserialnum=:mserialnum ";
			Query query = em.createQuery(jpql);
			query.setParameter("mplatform", platforminfo.getMplatform());
			query.setParameter("magent", platforminfo.getMagent());
			query.setParameter("mserialnum", platforminfo.getMserialnum());
			NewsPlatforminfo platforminfo_temp= (NewsPlatforminfo) getSingleResult(query);
			if(platforminfo_temp!=null){
				resultb=null;
			}else{
				em.persist(platforminfo);
			}
		} catch (Exception e) {
			logger.error("addNewsPlatforminfo fail !!!" + e.getMessage());
			resultb=false;
		} 
		return resultb;
	}

	public Object getSingleResult(Query query) {
		Object o =null;
		try {
			o = query.getSingleResult();
		} catch (NoResultException ex) {
			return null;
		} catch (NonUniqueResultException ex) {
			o = query.getResultList();
			return ((List) o).get(0);
		}
		return o;
	}
	public static boolean StringIsNotNull(Object str) {
        boolean b = false;
        try {
            if (!str.equals("") && !str.equals("null")) {
                b = true;
            }
        } catch (Exception e) {
        }
        return b;
    }
	
	

}
